(function($) {
    // définition du plugin jQuery
	$.fn.myBox = function(params,callback) {	
		var params;
		var tour = 1;
		var deg = 10;
		var interval = 10;
		var comptTour = (tour*360/deg);
		var degre;
		var widthInterval;
		var heightInterval;		
		var widthInit;
		var heightInit;
		var topRotate;
		var leftRotate;
		var tempTop ;
		var tempLeft;
		
		$(this).click(function(e){
			var current = $(this);
			var p = current.position();
			// Fusionner les paramètres par défaut et ceux de l'utilisateur
			params = $.extend( {
			maskColor : "#ccc",
			maskOpacity : '0.5',
			BoxBgColor : "#fff",
			effect : 'zoom',
			type : 'image',
			clone : false,
			setContent : false,
			width : 600,
			height : 400
			}, params);

			tempX = e.pageX;
			tempY = e.pageY;
			
			
			maskWidth = $(window).width();
			maskHeight = $(window).height();
			maskPosition = 'fixed';
			//Fix for IE
			if($.browser.msie){
				maskWidth = $(document).width();
				maskHeight = $(document).height();
				maskPosition = 'absolute';
			}
				
			$("<div>", {
				id: "myMask",
				css: {
					height: maskHeight,
					width: maskWidth,
					backgroundColor: params.maskColor,
					display : 'none',
					position : maskPosition,
					top : 0,
					left : 0,
					opacity : params.maskOpacity
				},
				click: function() {
				   closeBox(params.effect,tempX,tempY,params.setContent,params.clone,current);
				}
			}).appendTo("body");
						
			$("<div>", {
				id: "myBox",
				css: {
					height: 0,
					width: 0,
					backgroundColor: params.BoxBgColor,
					position : 'absolute',
					top : tempY, 
					left : tempX,
					boxShadow : '0 0 5px #000'
				}
			}).appendTo("body");
			
			$("<div>", {
				id: "myContent",
				css : {
					display : 'none'
				}
			}).appendTo("#myBox");
			
			var closeTop = "-16px";
			var closeRight = "-16px";
			var closeImg = "fermer.png";
			//Fix for IE
			if($.browser.msie){
				closeTop = "0px";
				closeRight = "5px";
				closeImg = "IE_fermer.gif";
			}
			
			$("<div>", {
				id: "myClose",
				css : {
					position : 'absolute',
					top : closeTop,
					right : closeRight,
					display : 'none',
					cursor : 'pointer'
				}
			})
			.click(function(){
				closeBox(params.effect,tempX,tempY,params.setContent,params.clone,current);
			})
			.html('<img src="'+closeImg+'">')
			.appendTo("#myBox");
			
			if(params.setContent)
				setContent(params.type,current,false);
				
			var width;
			var height;
			if(params.type == 'image'){
				src = "";
				
				if(current.tagName() == "a")
					src = current.attr("href");
				else
					src = current.attr("src");
								
				$("<img>", {
					id: "tempImgJustForReelSize",
					css : {
						display : 'none'
					}
				}).appendTo("body").error(function(){
					remove();
					return false;
				}).load(function(){
					width = $(this).width();
					height = $(this).height();
					
					var maxWidth = $(window).width()-200;
					var maxHeight = $(window).height()-200;
					
					if(width > maxWidth){
						var coef = maxWidth/width;
						width = width * coef;
						height = height * coef;
					}
						
					if(height > maxHeight){
						var coef = maxHeight/height;
						width = width * coef;
						height = height * coef;
					}
				}).attr("src",src);			

					
			}else{
				width = params.width;
				height = params.height;
			}
			//alert((($(window).height()/2)-(height/2) + $(window).scrollTop()) +'px');
			if (params.effect == 'zoom'){
				if (params.clone)
				$('#myBox').css({
					top : p.top,
					left : p.left,
					width : current.width(),
					height : current.height()
				});
				
				$('#myMask').fadeIn(500,function(){
					$('#myBox').animate({
						width : width,
						height : height,
						top : (($(window).height()/2)-(height/2) + $(window).scrollTop()) +'px',
						left : (($(window).width()/2)-(width/2) + $(window).scrollLeft()) +'px'
					},function(){
						if(!params.setContent)
							setContent(params.type,current);
					});
				});
			}else if (params.effect == 'rotate'){
				$('#myBox').css(
					{
						top : ($(window).height()/2),
						left : ($(window).width()/2)
					});
				if (params.clone)
					$('#myBox').css({
						top : p.top,
						left : p.left,
						width : current.width(),
						height : current.height()
					});
				$('#myMask').fadeIn(500,function(){
					openRotate(0,width,height,params.clone,current,function(){setContent(params.type,current,params.setContent);});
				});
			}else if (params.effect == 'fade'){
				$('#myMask').fadeIn(500,function(){
					$('#myBox').css({
						width : width,
						height : height,
						top : (($(window).height()/2)-(height/2) + $(window).scrollTop()) +'px',
						left : (($(window).width()/2)-(width/2) + $(window).scrollLeft()) +'px'
					});
					$('#myBox').fadeIn(500,function(){
						if(!params.setContent)
							setContent(params.type,current);
					});
				});	
			}

			return false;	
			
		});	
		

		closeBox = function(effect,cX,cY,setContent,clone,current){
		
			$('#myClose').fadeOut(500);			
			
			if(effect == 'zoom'){
				p = current.position();
				var w = 0;
				var h = 0;
				var t = cY;
				var l = cX;
				
				if(clone){
					t = p.top;
					l = p.left;
					w = current.width();
					h = current.height();
				}
				
				if(!setContent)
					$('#myContent').fadeOut(500,function(){
						$('#myBox').animate({
								width : w,
								height : h,
								top : t, 
								left : l
						},function(){
							remove();
						});
					});
				else
					$('#myBox').animate({
							width : w,
							height : h,
							top : t, 
							left : l
					},function(){
						remove();
					});
			}else if (effect == 'rotate'){
				if(!setContent)
					$('#myContent').fadeOut(500,function(){
						closeRotate(0,$('#myBox').width(),$('#myBox').height(),clone,current, remove);
					});
				else
					closeRotate(0,$('#myBox').width(),$('#myBox').height(),clone,current, remove);
			}else if (effect == 'fade'){
				if(!setContent)
					$('#myContent').fadeOut(500,function(){
						$('#myBox').fadeOut(500,function(){
							remove();
						});
					});
				else
					$('#myBox').fadeOut(500,function(){
						remove();
					});
			}	
		}
		
		remove = function(){
			
			$('#myMask').fadeOut(500,function(){
				$('#myBox').remove();
				$('#myMask').remove();
			});

			return "";
		}
		
		openRotate = function(x,width,height,clone,current,callback){
			if (x >= comptTour){
				return callback();
			}	
			if(x==0){
				degre = 0;
				widthInterval = 0;
				heightInterval = 0;
				if (clone){
					widthInit = current.width();
					heightInit = current.height();
					topRotate = current.position().top;
					leftRotate = current.position().left;
					widthInterval = widthInit;
					heightInterval = heightInit;
				}
			}	
			
			
			degre = degre + deg;
			
			if (clone){
				widthInterval = widthInterval + (width-widthInit)/comptTour;
				heightInterval = heightInterval + (height-heightInit)/comptTour;
				var topMax = (($(window).height()/2)-(height/2) + $(window).scrollTop());
				var leftMax = (($(window).width()/2)-(width/2) + $(window).scrollLeft());
				var topInterval = (topMax-current.position().top) / comptTour;
				var leftInterval = (leftMax-current.position().left) / comptTour;
				topRotate = topRotate + topInterval;
				leftRotate = leftRotate + leftInterval;
				
			}else{
				widthInterval = widthInterval + width/comptTour;
				heightInterval = heightInterval + height/comptTour;
				topRotate = (($(window).height()/2)-(heightInterval/2) + $(window).scrollTop()) +'px';
				leftRotate = (($(window).width()/2)-(widthInterval/2) + $(window).scrollLeft()) +'px';
			}
			
			$('#myBox').css({ WebkitTransform: 'rotate('+degre+'deg)'});
			// For Mozilla browser: e.g. Firefox
			$('#myBox').css({ '-moz-transform': 'rotate('+degre+'deg)'});	
			$('#myBox').css(
			{
				width : widthInterval + "px",
				height : heightInterval + "px",
				top : topRotate,
				left : leftRotate
			});
			
			x++;
			setTimeout(function(){openRotate(x,width,height,clone,current,callback);},interval);	
		}
		
		closeRotate = function(x,width,height,clone,current,callback){
			temp = 1;
			if (x >= comptTour)
				return callback();
			
			if(x==0){
				degre = 0;
				widthInterval = width;
				heightInterval = height;
				if (clone){
					widthInit = current.width();
					heightInit = current.height();
					topRotate = $('#myBox').position().top;
					leftRotate = $('#myBox').position().left;
					tempTop = $('#myBox').position().top;
					tempLeft = $('#myBox').position().left;
				}
			}		
			
			degre = degre - deg;
			
			if (clone){
				widthInterval = widthInterval - (width-widthInit)/comptTour;
				heightInterval = heightInterval - (height-heightInit)/comptTour;
				var topMax = current.position().top;
				var leftMax = current.position().left;
				var topInterval = (tempTop-topMax) / comptTour;
				var leftInterval = (tempLeft-leftMax) / comptTour;
				topRotate = topRotate - topInterval;
				leftRotate = leftRotate - leftInterval;
				//alert("topRotate = "+topRotate+" topInterval = "+topInterval+" topMax = "+topMax);
				
			}else{
				widthInterval = widthInterval - width/comptTour;
				heightInterval = heightInterval - height/comptTour;
				topRotate = (($(window).height()/2)-(heightInterval/2) + $(window).scrollTop()) +'px';
				leftRotate = (($(window).width()/2)-(widthInterval/2) + $(window).scrollLeft()) +'px';
			}
			$('#myBox').css({ WebkitTransform: 'rotate('+degre+'deg)'});
			// For Mozilla browser: e.g. Firefox
			$('#myBox').css({ '-moz-transform': 'rotate('+degre+'deg)'});	
			$('#myBox').css(
			{
				width : widthInterval + "px",
				height : heightInterval + "px",
				top : topRotate,
				left : leftRotate
			});
			
			x++;
			setTimeout(function(){closeRotate(x,width,height,clone,current,callback);},interval);	
		}
		
		setContent = function(type,obj,setContent){
			if(!setContent){
				if( obj.tagName() == 'img' && type == "image"){
					$('#myContent').html('<img src="'+obj.attr('src')+'" width="100%">').fadeIn(500);
				}else if( obj.tagName() == 'a' && type == "image"){
					$('#myContent').html('<img src="'+obj.attr('href')+'" width="100%">').fadeIn(500);
				}else if (type='url'){
					url = obj.attr('href');
					$('#myContent').load(url).fadeIn(500);
				}
			}	
			
			$('#myClose').fadeIn(500);
		}
	// Permettre le chaînage par jQuery
	return this;
	};
})(jQuery);


(function($) {
	$.fn.tagName = function() {
	   return this.get(0).tagName.toLowerCase();
	}
})(jQuery);



